package cn.niit.pm.controller;


import cn.niit.pm.entity.Employee;
import cn.niit.pm.entity.Recruit;
import cn.niit.pm.entity.Salary;
import cn.niit.pm.service.SalaryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 薪资控制器
 *
 * @author xiaomi
 */
@Slf4j
@Api("薪资控制器")
@RequestMapping("salary")
@Controller
public class SalaryController {
    @Autowired
    private SalaryService salaryService;

    /**
     * 薪资列表首页，主要用于显示薪资列表
     * @return
     */
    @GetMapping("")
    public String payroll(Salary salary, Model model){
        List<Salary> salaryList = salaryService.selectListBySalary(salary);
        model.addAttribute("salaryList", salaryList);
        return "salary/payroll";
    }

    /**
     *进入 修改 薪资页面
     * @param model
     * @param salaryId
     * @return
     */
    @GetMapping("/{id}")
    public String saveRecruitPlan(Model model, @PathVariable("id") Integer salaryId){
        System.out.println(salaryId);
        Salary salary = null;
        if(null != salaryId && salaryId > 0){
            salary = salaryService.selectBysalaryId(salaryId);
        }else{
        }
        model.addAttribute("salary", salary);
        return "salary/payroll";
    }

    /**
     * 更新薪资列表
     * @param salary
     * @return
     */
    @PutMapping("")
    public String update(Salary salary, Employee employee, HttpServletRequest request){
        //拿用户数据
        Employee loginEmployee = (Employee) request.getSession().getAttribute("employee");
        log.info("待更新的用户：{}", employee);
        //为修改的记录赋值更新人信息
        employee.setModifyUserId(loginEmployee.getEmployeeId());
        employee.setModifyUserName(loginEmployee.getEmployeeName());
        employee.setGmtCreate(new Date());
        int rows = salaryService.updateSalary(salary);
        log.info("被更新的记录数:{}", rows);
        if (rows <= 0){
            return "recruit/addrecruitplan";
        }
        return "redirect:/recruitplan";
    }
}
